Skip to content

Glasgow | 26-SDC-Mar | Mohammed Abdoon | Sprint 2 | Improve Code With Caches - #221

Open
M-Abdoon wants to merge 1 commit into
CodeYourFuture:mainfrom
M-Abdoon:sprint2/improve_with_caches
Open

Glasgow | 26-SDC-Mar | Mohammed Abdoon | Sprint 2 | Improve Code With Caches#221
M-Abdoon wants to merge 1 commit into
CodeYourFuture:mainfrom
M-Abdoon:sprint2/improve_with_caches

Conversation

@M-Abdoon

Copy link
Copy Markdown

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

@M-Abdoon M-Abdoon added 📅 Sprint 2 Assigned during Sprint 2 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Module-Complexity The name of the module. labels Jul 16, 2026
Comment on lines +1 to +10
cache = {}

def fibonacci(n):
if n in cache:
return cache[n]
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
result = fibonacci(n - 1) + fibonacci(n - 2)
cache[n] = result
return result

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is correct.

Could you look into an approach that doesn't require keeping the cache in the global scope?

Comment on lines +19 to +24
coin = coins[0]
for count in range(total // coin + 1):
intermediate = ways_to_make_change_helper(total - coin * count, coins[1:])
ways += intermediate

cache[key] = ways

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array and tuple creations are relatively costly operations.

coins can only be one of the following 9 tuples:

(200, 100, 50, 20, 10, 5, 2, 1)
(100, 50, 20, 10, 5, 2, 1)
(50, 20, 10, 5, 2, 1)
...
(1)
()

We could further improve the performance if we can

  • avoid repeatedly creating the same sub-tuples at line 21 (e.g. use another cache), and
  • create key as (total, a_unique_integer_identifying_the_subarray) instead of as (total, tuple of coins)
    • There are only a small number of different subarrays. We can easily assign each subarray a unique integer.

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Module-Complexity The name of the module. Reviewed Volunteer to add when completing a review with trainee action still to take. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants